package mars;

import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 封装HttpClient
 * 保持session会话
 */
public class HC extends Base {
    public static CloseableHttpClient httpClient = null;
    public static HttpClientContext context = null;
    public static RequestConfig requestConfig = null;
    public static CookieStore cookieStore = new BasicCookieStore();
    public static List<Header> headers = null;
    static {
        if (context == null) {
            context = HttpClientContext.create();
            requestConfig = RequestConfig.DEFAULT;
            initHeader();
//            addCookie("UM_distinctid","15f38c5ea2c3c-0bc5a9de65a05f-742b6434-7d938-15f38c5ea2d9f8");
//            addCookie("CNZZDATA1256571064","1418848175-1508482763-%7C1508482763");
            httpClient = HttpClientBuilder.create().setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
                    .setDefaultCookieStore(cookieStore)
                    .setDefaultHeaders(headers).build();

        }
    }

    private static void initHeader(){
        headers = new ArrayList<Header>();
//        headers.add(new BasicHeader("Content-Type","text/html;charset=utf-8r"));
//        headers.add(new BasicHeader("Accept-Encoding","gzip, deflate, br"));
//        headers.add(new BasicHeader("Accept-Language","zh-CN,zh;q=0.8"));
//        headers.add(new BasicHeader("Cache-Control","max-age=0"));
//        headers.add(new BasicHeader("Connection","keep-alive"));
//        headers.add(new BasicHeader("Host","www.jintouhui.com"));
//        headers.add(new BasicHeader("Upgrade-Insecure-Requests","1"));
//        headers.add(new BasicHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Mobile Safari/537.36"));
    }



    public static void addCookie(String key,String value){
        BasicClientCookie cookie =new BasicClientCookie(key,value);
        cookie.setDomain("www.jintouhui.com");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
    }

    /**
     * http get
     *
     * @param url
     * @return
     */
    public static CloseableHttpResponse get(String url) {
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            return response;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Post method
     *
     * @param url
     * @param parameter
     * @return
     */
    public static CloseableHttpResponse post(String url, HashMap<String, String> parameter) {
        for(Cookie cookie : cookieStore.getCookies()){
            System.out.println(cookie.getName()+":"+cookie.getValue());
        }
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
        //httpPost.addhe
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            if (parameter != null) {
                for (String key : parameter.keySet()) {
                    NameValuePair nvp = new BasicNameValuePair(key, parameter.get(key));
                    params.add(nvp);
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            response = httpClient.execute(httpPost);
            return response;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
